1
|
|
|
import path from 'path' |
2
|
|
|
import { |
3
|
|
|
coreUtils, |
4
|
|
|
cmsTemplates, |
5
|
|
|
cmsOperations, |
6
|
|
|
config, |
7
|
|
|
abeExtend, |
8
|
|
|
cmsData, |
9
|
|
|
Manager |
10
|
|
|
} from '../../' |
11
|
|
|
|
12
|
|
|
var create = function(template, pathCreate, name, req, forceJson = {}, duplicate = false) { |
13
|
|
|
var p = new Promise((resolve, reject) => { |
14
|
|
|
abeExtend.hooks.instance.trigger('beforeCreate', template, pathCreate, name, req, forceJson) |
15
|
|
|
|
16
|
|
|
var templatePath = path.join(config.root, config.templates.url, `${template}.${config.files.templates.extension}`) |
17
|
|
|
var filePath = path.join(pathCreate, name) |
18
|
|
|
filePath = coreUtils.slug.clean(filePath) |
19
|
|
|
filePath = path.join(config.root, config.draft.url, filePath.replace(config.root,'')) |
20
|
|
|
if(templatePath !== null && filePath !== null) { |
21
|
|
|
var tplUrl = cmsData.file.fromUrl(filePath) |
22
|
|
|
|
23
|
|
|
if(!coreUtils.file.exist(tplUrl.json.path)) { |
24
|
|
|
var json = (forceJson) ? forceJson : {} |
25
|
|
|
var tpl = templatePath |
26
|
|
|
var text = cmsTemplates.template.getTemplate(tpl) |
27
|
|
|
if (duplicate) { |
28
|
|
|
json = cmsData.values.removeDuplicate(text, json) |
29
|
|
|
} |
30
|
|
|
text = cmsData.source.removeDataList(text) |
31
|
|
|
var resHook = abeExtend.hooks.instance.trigger('beforeFirstSave', filePath, req.query, json, text) |
32
|
|
|
filePath = resHook.filePath |
33
|
|
|
json = resHook.json |
34
|
|
|
text = resHook.text |
35
|
|
|
|
36
|
|
|
abeExtend.hooks.instance.trigger('afterCreate', json, text, pathCreate, name, req, forceJson) |
37
|
|
|
cmsOperations.save.save(filePath, template, json, text, 'draft', null, 'draft') |
38
|
|
|
.then((resSave) => { |
39
|
|
|
Manager.instance.updatePostInList(resSave.jsonPath) |
40
|
|
|
filePath = resSave.htmlPath |
41
|
|
|
tplUrl = cmsData.file.fromUrl(filePath) |
42
|
|
|
resolve(resSave.json) |
43
|
|
|
}).catch(function(e) { |
44
|
|
|
reject() |
45
|
|
|
console.error(e) |
46
|
|
|
}) |
47
|
|
|
}else { |
48
|
|
|
json = cmsData.file.get(tplUrl.json.path) |
49
|
|
|
resolve(json, tplUrl.json.path) |
50
|
|
|
} |
51
|
|
|
}else { |
52
|
|
|
reject() |
53
|
|
|
} |
54
|
|
|
}).catch(function(e) { |
55
|
|
|
console.error(e) |
56
|
|
|
}) |
57
|
|
|
|
58
|
|
|
return p |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
export default create |